﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.IO;

namespace SimpleDelphiExpert
{
    public class StreamWrapper : IStream, UCOMIStream
    {
        public StreamWrapper(MemoryStream stream)
        {
            if (stream == null)
                throw new ArgumentNullException("stream", "Can't wrap null stream.");
            this.stream = stream;
        }

        private MemoryStream stream;

        public void Read(byte[] pv, int cb, System.IntPtr pcbRead)
        {
            Marshal.WriteInt32(pcbRead, (Int32)stream.Read(pv, 0, cb));
        }

        public void Seek(long dlibMove, int dwOrigin, System.IntPtr plibNewPosition)
        {
            Marshal.WriteInt32(plibNewPosition, (int)stream.Seek(dlibMove, (SeekOrigin)dwOrigin));
        }


        public void Clone(out IStream ppstm)
        {
            throw new NotImplementedException();
        }

        public void Commit(int grfCommitFlags)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
        {
            throw new NotImplementedException();
        }

        public void LockRegion(long libOffset, long cb, int dwLockType)
        {
            throw new NotImplementedException();
        }

        public void Revert()
        {
            throw new NotImplementedException();
        }

        public void SetSize(long libNewSize)
        {
            throw new NotImplementedException();
        }

        public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag)
        {
            throw new NotImplementedException();
        }

        public void UnlockRegion(long libOffset, long cb, int dwLockType)
        {
            throw new NotImplementedException();
        }

        public void Write(byte[] pv, int cb, IntPtr pcbWritten)
        {
            throw new NotImplementedException();
        }

        public void Clone(out UCOMIStream ppstm)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(UCOMIStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
        {
            throw new NotImplementedException();
        }

        public void Stat(out System.Runtime.InteropServices.STATSTG pstatstg, int grfStatFlag)
        {
            pstatstg = new System.Runtime.InteropServices.STATSTG();
            pstatstg.type = 2; //STGTY_STREAM
            pstatstg.cbSize = stream.Length;
            pstatstg.mtime.dwHighDateTime = 0;
            pstatstg.mtime.dwLowDateTime = 0;
            pstatstg.atime.dwHighDateTime = 0;
            pstatstg.atime.dwLowDateTime = 0;
            pstatstg.ctime.dwHighDateTime = 0;
            pstatstg.ctime.dwLowDateTime = 0;
            pstatstg.grfLocksSupported = 1;//LOCK_WRITE = 1 
        }
    }
}
